﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Drawing;

namespace Spencen.Mobile.UI.Transforms
{
    public class RotateTransform : Transform
    {
        public RotateTransform() : this( 0.0f ) { }

        public RotateTransform( float angle )
        {
            Angle = angle;
        }

        public float Angle { get; set; }

        public override Point[] TransformedPoints( Point[] points, Point center )
        {
            return TransformedPoints( points, center, Angle );
        }

        public override Point[] TransformedPointsReverse( Point[] transformedPoints, Point center )
        {
            return TransformedPoints( transformedPoints, center, -Angle );
        }

        private static Point[] TransformedPoints( Point[] points, Point center, float angle )
        {
            var rotatedPoints = new Point[ points.Length ];
            var angleInRadians = angle / 180 * Math.PI;
            for ( int index = 0; index < points.Length; index++ )
            {
                var point = points[ index ];
                rotatedPoints[ index ].X = (int)( ( point.X - center.X ) * (float)Math.Cos( angleInRadians ) - ( point.Y - center.Y ) * (float)Math.Sin( angleInRadians ) ) + center.X;
                rotatedPoints[ index ].Y = (int)( ( point.X - center.X ) * (float)Math.Sin( angleInRadians ) + ( point.Y - center.Y ) * (float)Math.Cos( angleInRadians ) ) + center.Y;
            }
            return rotatedPoints;
        }
    }
}
